﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace QLNS
{
    public partial class frmChucvu : Form
    {
        public frmChucvu()
        {
            InitializeComponent();
        }
        public SqlConnection getConnect()
        {
            return new SqlConnection(@"Data Source=THANGVUONG\SQLEXPRESS;Initial Catalog=Quanlynhansu;Integrated Security=True");
        }
        public DataTable Chucvu_SelectAll()
        {
            string sql = "Select * from ChucVu";
            SqlConnection conn = getConnect();
            SqlDataAdapter adap = new SqlDataAdapter(sql, conn);
            DataTable dt = new DataTable();
            adap.Fill(dt);
            return (dt);
        }
        public DataTable Chucvu_Search(Chucvu cv)
        {
            SqlConnection conn = getConnect();
            conn.Open();
            SqlCommand cmd = new SqlCommand("Chucvu_Search", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@Macv", cv.MaCV));
            SqlDataAdapter adap = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            adap.Fill(dt);
            conn.Close();
            return dt;
        }
        public void Chucvu_Insert(Chucvu cv)
        {
            SqlConnection Connect = getConnect();
            Connect.Open();
            SqlCommand cmd = new SqlCommand("Chucvu_Insert", Connect);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@Macv", cv.MaCV));
            cmd.Parameters.Add(new SqlParameter("@Tencv", cv.TenCV));
            cmd.ExecuteNonQuery();
            Connect.Close();
        }
        public void Chucvu_Update(Chucvu cv)
        {
            SqlConnection Connect = getConnect();
            Connect.Open();
            SqlCommand cmd = new SqlCommand("Chucvu_Update", Connect);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@Macv", cv.MaCV));
            cmd.Parameters.Add(new SqlParameter("@Tencv", cv.TenCV));
            cmd.ExecuteNonQuery();
            Connect.Close();
        }
        public void Chucvu_Delete(string Macv)
        {
            SqlConnection Connect = getConnect();
            Connect.Open();
            SqlCommand cmd = new SqlCommand("Chucvu_Delete", Connect);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@Macv", Macv));
            cmd.ExecuteNonQuery();
            Connect.Close();
        }

        private void frmChucvu_Load(object sender, EventArgs e)
        {
            grvChucvu.DataSource = Chucvu_SelectAll();
        }

        private void btThem_Click(object sender, EventArgs e)
        {
            if (txtMaCV.Text == "")
            {
                MessageBox.Show("Bạn phải nhập mã chức vụ!");
                txtMaCV.Focus();
                return;
            }
            Chucvu cv = new Chucvu();
            cv.MaCV = txtMaCV.Text;
            cv.TenCV = txtTenCV.Text;
            try
            {
                Chucvu_Insert(cv);
                grvChucvu.DataSource = Chucvu_SelectAll();
            }
            catch { MessageBox.Show("Không thêm được"); }
        }

        private void btSua_Click(object sender, EventArgs e)
        {
            if (txtMaCV.Text == "")
            {
                MessageBox.Show("Bạn phải nhập mã chức vụ!");
                txtMaCV.Focus();
                return;
            }
            Chucvu cv = new Chucvu();
            cv.MaCV = txtMaCV.Text;
            cv.TenCV = txtTenCV.Text;
            try
            {
                Chucvu_Update(cv);
                grvChucvu.DataSource = Chucvu_SelectAll();
            }
            catch { MessageBox.Show("Không sửa được"); }
        }

        private void btXoa_Click(object sender, EventArgs e)
        {
            if (txtMaCV.Text == "")
            {
                MessageBox.Show("Bạn phải nhập mã chức vụ!");
                txtMaCV.Focus();
                return;
            }
            Chucvu_Delete(txtMaCV.Text);
            grvChucvu.DataSource = Chucvu_SelectAll();
        }

        private void btThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
